package qlsl.androiddesign.view.subview.fragmentview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import qlsl.androiddesign.activity.subactivity.RecruitActivity;
import qlsl.androiddesign.activity.subactivity.RecruitDetailActivity;
import qlsl.androiddesign.adapter.subadapter.JobRecruitAdapter;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.RecruitJob;
import qlsl.androiddesign.fragment.subfragment.RecruitFragment;
import qlsl.androiddesign.http.service.subservice.JobService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshListView;
import qlsl.androiddesign.listener.OnPullRefreshListener;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 招聘页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class RecruitView extends FunctionView<RecruitActivity> {

	private PullToRefreshListView refreshView;

	private RecruitFragment fragment;

	private List<RecruitJob> list = new ArrayList<RecruitJob>();

	private Pager pager;

	public RecruitView(RecruitFragment fragment, ViewGroup rootView,
			ViewGroup contentView) {
		super((RecruitActivity) fragment.getActivity());
		this.fragment = fragment;
		setContentView(rootView, contentView);
	}

	protected void initView(View view) {
		hideTitleBar();
		refreshView = findViewById(R.id.refreshListView);
	}

	protected void initData() {
		queryJobList(1, "武汉");
	}

	protected void initListener() {
		refreshView.setOnRefreshListener(refreshListener);
	}

	public <T> void showData(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<RecruitJob> list_result = (List<RecruitJob>) map.get("list");
			pager = (Pager) map.get("pager");
			String city = (String) map.get("city");
			if (pager.getPageNo() == 1) {
				resetList(list_result);
			} else {
				list.addAll(list_result);
			}
			notifyDataSetChanged(city);
		}
	}

	private void notifyDataSetChanged(String city) {
		JobRecruitAdapter adapter = (JobRecruitAdapter) refreshView
				.getBaseAdapter();
		if (adapter == null) {
			adapter = new JobRecruitAdapter(activity, list);
			refreshView.setAdapter(adapter);
			adapter.setCity(city);
		} else {
			adapter.setCity(city);
			adapter.notifyDataSetChanged();
		}
		refreshView.onRefreshComplete();
	}

	public void queryJobList(int page, String city) {
		JobService.queryJobList(page, "1", city, this, fragment);
	}

	private void resetList(List<RecruitJob> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.list_item:
			doClickListItem(view);
			break;
		}
	}

	private void doClickListItem(View view) {
		int position = refreshView.getRefreshableView()
				.getPositionForView(view);
		RecruitJob recruit = (RecruitJob) refreshView.getAdapter().getItem(
				position);
		activity.startActivity(recruit, RecruitDetailActivity.class);
	}

	private OnPullRefreshListener<ListView> refreshListener = new OnPullRefreshListener<ListView>() {

		public void onPullDown() {
			refreshView.onRefreshComplete();
			// queryJobList(1, "");
		}

		public void onPullUp() {
			super.onPullUp(refreshView, pager);
		}

		public void nextPager() {
			// queryJobList(pager.getPageNo() + 1, "" + Constant.city);
		}
	};

}
